package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.model.MarketBrand;
import com.cskaoyan.model.MarketBrandExample;
import com.cskaoyan.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Pudding
 * @Date 2024/6/11 20:12
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    MarketBrandMapper brandMapper;

    @Override
    public List<MarketBrand> list(int page, int brandLimit) {
        //1. 开启分页
        PageHelper.startPage(page, brandLimit);
        //2. 构造查询条件
        MarketBrandExample example = new MarketBrandExample();
        //2.1 添加没有逻辑删除的条件
        example.createCriteria().andDeletedEqualTo(false);
        //2.2 按添加时间降序排序
        example.setOrderByClause("add_time desc");

        //3. 进行查询
        List<MarketBrand> brandList = brandMapper.selectByExample(example);
        return brandList;
    }

    @Override
    public MarketBrand findByBrandId(Integer brandId) {
        //1. 构造查询条件
        MarketBrandExample example = new MarketBrandExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andIdEqualTo(brandId);

        //2. 进行查询
        List<MarketBrand> brandList = brandMapper.selectByExample(example);

        //3. 返回查询的结果
        return brandList != null && brandList.size() == 1 ? brandList.get(0) : null;
    }
}
